package com.example.demo01.Utils;


import com.example.demo01.VO.IPacketInstruction;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
@Lazy(value = false)
public class FactoryPacketInstruction implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Map<String, IPacketInstruction> packetInstructionMap=new ConcurrentHashMap<>();




    public IPacketInstruction getPacketInstruction(String telNo){

        if(!packetInstructionMap.containsKey(telNo)) return null;

        return packetInstructionMap.get(telNo);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        Map<String, IPacketInstruction> packetInstructionMaps =applicationContext.getBeansOfType(IPacketInstruction.class);

        packetInstructionMaps.values().forEach(x-> {

            if(!x.getTelNo().isEmpty()){
                packetInstructionMap.put(x.getTelNo(),x);
            }
        });
    }
}
